// pages/jobDetail/jobDetail.js
const call = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: [],
    store: false,
    name: '',
    avatar: '',
    recruiter: '',
    price: '',
    recruitnum: 0,
    instru: '',
    detail: '',
    contact: '',
    tel: '',
    adress: ''
  },

  handleImagePreview: function (e) {
    console.log("handleImagePreview", e)
    const idx = e.target.dataset.idx
    const banner = this.data.banner
    wx.previewImage({
      current: banner[idx], // 当前显示图片的http链接
      urls: banner // 需要预览的图片http链接列表
    })
  },

  handleStore: function (){
    const that = this;
    const jobId = this.data.jobId
    const openId = wx.getStorageSync('token');
    if(!openId){
      // 弹窗，让用户授权
      wx.navigateTo({
        url: '../authPage/authPage'
      })
    } else{
      call.requestGet('jus/goStore',{
        openId: openId,
        id: jobId
      },function(e){
        that.setData({
          store: !that.data.store
        })
      },function(){
        wx.showToast({
          title: '操作失败',
        })
      })
    }
  },

  toCall: function() {
    wx.makePhoneCall({
      phoneNumber: this.data.tel
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      jobId: options.jobId
    })
    wx.setStorageSync('jobId', options.jobId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const jobId = this.data.jobId
    const openId = wx.getStorageSync('token') || '';
    const that = this
    call.requestGet('jus/findOne', { 
      openId: openId, 
      id: jobId 
    }, function(e){
      console.log('jobDetail', e)
      that.setData({
        banner: e.imgs,
        store: e.store,
        name: e.name,
        avatar: e.avatar,
        recruiter: e.nickName,
        price: e.price,
        recruitnum: e.recruitnum,
        instru: e.instru,
        detail: e.detail,
        contact: e.contact,
        tel: e.tel,
        adress: e.adress
      })
    }, function () { 
      
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})